package com.atgm.mall.gmmallclub.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atgm.common.exception.BizCodeEnume;
import com.atgm.common.exception.ClubException;
import com.atgm.common.utils.GmMallUtils;
import com.atgm.common.utils.PageUtils;
import com.atgm.common.utils.Query;
import com.atgm.common.utils.R;
import com.atgm.common.vo.member.MemberInfoVo;
import com.atgm.mall.gmmallclub.dao.ArticleCommentDao;
import com.atgm.mall.gmmallclub.entity.ArticleCommentEntity;
import com.atgm.mall.gmmallclub.feign.MemberFeignService;
import com.atgm.mall.gmmallclub.service.ArticleCommentService;
import com.atgm.mall.gmmallclub.vo.resp.ArticleCommentRespVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ArticleCommentServiceImpl extends ServiceImpl<ArticleCommentDao, ArticleCommentEntity> implements ArticleCommentService {

    @Autowired
    private MemberFeignService memberFeignService;

    @Override
    public PageUtils queryCommentInfoByArticleId(Map<String, Object> params) {
        String articleId = (String) params.get("articleId");
        if (GmMallUtils.isEmpty(articleId)) {
            return null;
        }
        QueryWrapper<ArticleCommentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id", articleId);
        queryWrapper.orderByDesc("create_time");

        IPage<ArticleCommentEntity> page = this.page(new Query<ArticleCommentEntity>().getPage(params), queryWrapper);
        List<ArticleCommentEntity> articleCommentEntityList = page.getRecords();
        PageUtils pageUtils = new PageUtils(page);

        if (GmMallUtils.isEmpty(articleCommentEntityList)) {
            return pageUtils;
        }

        List<ArticleCommentRespVo> articleCommentRespVos = new LinkedList<>();
        articleCommentEntityList.forEach(articleCommentEntity -> {
            ArticleCommentRespVo articleCommentRespVo = new ArticleCommentRespVo();
            BeanUtils.copyProperties(articleCommentEntity, articleCommentRespVo);
            articleCommentRespVos.add(articleCommentRespVo);
        });

        //获取用户id
        List<Long> memberIds = articleCommentEntityList.stream().map(ArticleCommentEntity::getMemberId).collect(Collectors.toList());
        R r = memberFeignService.getMemberInfo(memberIds);
        List<MemberInfoVo> memberInfoVoList = new ArrayList<>();
        if (0 == r.getCode()) {
            memberInfoVoList = r.getData(new TypeReference<List<MemberInfoVo>>() {});
        }

        if (GmMallUtils.isNotEmpty(memberInfoVoList)) {
            List<MemberInfoVo> finalMemberInfoVoList = memberInfoVoList;
            articleCommentRespVos.forEach(articleCommentRespVo -> {
                articleCommentRespVo.setNickname(finalMemberInfoVoList.stream().filter(memberInfoVo -> {
                    return memberInfoVo.getId().equals(articleCommentRespVo.getMemberId());
                }).findAny().get().getNickname());
                articleCommentRespVo.setHeader(finalMemberInfoVoList.stream().filter(memberInfoVo -> {
                    return memberInfoVo.getId().equals(articleCommentRespVo.getMemberId());
                }).findAny().get().getHeader());
            });
        }
        pageUtils.setList(articleCommentRespVos);

        return pageUtils;
    }

    @Override
    public void saveArticleCommentInfo(ArticleCommentEntity articleCommentEntity) {
        articleCommentEntity.setReplyCount(0);
        articleCommentEntity.setThumbsCount(0);
        articleCommentEntity.setCreateTime(new Date());
        log.info("articleCommentEntity ： {}", articleCommentEntity);
        int count = baseMapper.insert(articleCommentEntity);
        if (1 != count) {
            throw new ClubException("评论失败", BizCodeEnume.CLUB_COMMENT.getCode());
        }
    }

    @Override
    public void updateThumbsCountInfo(Long id, Integer count) {
        QueryWrapper<ArticleCommentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "thumbs_count");
        queryWrapper.eq("id", id);
        ArticleCommentEntity articleCommentEntity = baseMapper.selectOne(queryWrapper);
        if (articleCommentEntity == null) {
            throw new ClubException("点赞失败", BizCodeEnume.MEMBER_THUMBS.getCode());
        }

        if (0 == count) {  //点赞数量 -1
            articleCommentEntity.setThumbsCount(articleCommentEntity.getThumbsCount() - 1);
        }else {
            articleCommentEntity.setThumbsCount(articleCommentEntity.getThumbsCount() + 1);
        }

        int update = baseMapper.updateById(articleCommentEntity);
        if (update == 0) {
            throw new ClubException("点赞失败", BizCodeEnume.MEMBER_THUMBS.getCode());
        }
    }
}
